package core;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.Timer;

import enemies.Infantry;
import enemies.Jeep;
import enemies.Tank;

public class WaveThread implements ActionListener, Runnable {
	
	private Model model;
	
	private Point2D.Double startingPoint;
	private ArrayList<Point2D.Double> route;
	
	private String waveType;
	private int amount;
	
	public WaveThread( Model model, int amount ){
		this.model = model;
		this.amount = amount;
		
		 //Eerste punt bepalen
		int random = (int)(Math.random() * this.model.getMap().getRoutes().size() );
		route = this.model.getMap().getRoutes().get( random );
			
		startingPoint = route.get( 0 );
		
		waveType = model.getFirstWave();
	}
	
	public void nextWave(){
		Timer t = new Timer( 1000, this );
		t.start();
	}
	
	public void run(){
		nextWave();
	}

	public void actionPerformed(ActionEvent arg0) {
		//ophalen welke mannen er moeten komen
		amount--;
		if( amount >= 0 )
			nextUnit();
		else {
			Timer t = (Timer)arg0.getSource();
			t.stop();
			t = null;
		}
	}
	
	public void nextUnit(){	
		int damage = (int)Math.pow(model.getStats().getWave(), 0.2) * model.getDifficulty();
		int speed = (int)Math.sqrt(model.getStats().getWave()) * 6 + 25;
		
		if( model.getBlizzard() )
			speed = speed/2;
		
		if( waveType.equals( "Tank" ) ){
			model.getEnemies().add( new Tank( route, new Point2D.Double(startingPoint.x, startingPoint.y), model.getStats().getWave() * 15 * model.getDifficulty(), speed, damage ) );
		} else if( waveType.equals( "Infantry" ) ){
			model.getEnemies().add( new Infantry( route, new Point2D.Double(startingPoint.x, startingPoint.y), model.getStats().getWave() * 12 * model.getDifficulty(), (int)(speed * 1.1), damage ) );
		} else if( waveType.equals( "Jeep" ) ){
			model.getEnemies().add( new Jeep( route, new Point2D.Double(startingPoint.x, startingPoint.y), model.getStats().getWave() * 9 * model.getDifficulty(), (int)(speed * 0.9), damage ) );
		}
	}

}
